<template>
	<view class="container bg-w">
		<view class="header uni-flex uni-row flex-between">
			<uni-nav-bar title="施工上报">
				<template slot='left'>
					<view class="flex width-100 ">
						<i class='iconfont iconfanhui' @click='goBack()'></i>
					</view>
				</template>
				<!-- <template slot='right'>
					<i class='iconfont iconcaidan right-i'></i>
					<view class=""></view>
				</template> -->
			</uni-nav-bar>
		</view>
		<view class="position-r-top">
			<view class="project-report">
				<!-- <uni-list>
					<uni-list-item title="基础信息" showArrow link :to="'/pages/Application/ConstructionReport/ConstructionProject/ProjectReport/BasicInformation/BasicInformation?projectId='+projectId"></uni-list-item>
				</uni-list> -->
				<view class="tabs">
					<uni-segmented-control :current="current" :values="itemstab" @clickItem="onClickItem" style-type="text" active-color="#09AAE9"></uni-segmented-control>
					<view class="content">
						<uni-list>
							<uni-list-item title="工序均分" :rightText='JSON.stringify(detailedList.score)'></uni-list-item>
							<uni-list-item title="工序进度" showArrow link @click="goProcessProgress"></uni-list-item>
							<view class="explain" v-show="detailedList.showOperationGuide == 1 || detailedList.showSafetyRegulations == 1 || detailedList.showMaterials == 1">
								<text class="order">说明</text>
								<!-- <uni-list-item v-show="detailedList.showOperationGuide == 1" title="操作指南" showArrow link to="/pages/Application/ConstructionReport/ConstructionProject/ProjectReport/Explain/Explain"></uni-list-item> -->
								<uni-list-item v-show="detailedList.showOperationGuide == 1" title="操作指南" link @click="OperationGuideBtn(detailedList.operationGuide)"
								 showArrow></uni-list-item>
								<uni-list-item v-show="detailedList.showSafetyRegulations == 1" title="安全规范" link @click="SafetyRegulationsBtn(detailedList.safetyRegulations)"
								 showArrow></uni-list-item>
								<uni-list-item v-show="detailedList.showMaterials == 1" title="物料清单" link @click="MaterialsBtn(detailedList.materials)"
								 showArrow></uni-list-item>
							</view>
							<view class="explain">
								<text class="order">上传资料</text>
								<view class="" v-for="(item,index) in detailedList.InfoList" :key='index'>
									<uni-list-item :title="item.gaugingPoint" :rightText='item.statusDesc' showArrow link @click="goConstructionData(item)"></uni-list-item>
								</view>
							</view>
						</uni-list>
						<view>
							<view class="text-a-c margin-t-20 margin-b-20">
								<text v-show="showBtn" class="theme-color vm" @click="AddConstruction(detailedList)">添加设备安装点</text>
								<icon v-show="showBtn" class="margin-l-10 vm" type="warn" size="18" @click="openTip"/>
							</view>
							<button v-show="showSub" @click="submitReviewBtn" class="submit-btn" type="submit">提交审核</button>
						</view>
					</view>
				</view>
			</view>
			<view class="">
				<uni-popup ref="popup" type="bottom">
					<view class="screen-popup">
						<view class="screen-popup-title flex flex-between">
							<text class="margin-l-10"> 请选择设备 </text>
							<view class="flex border-left">
								<text @click="close()">取消</text>
								<text @click="ChoiceTemplate()">确认</text>
							</view>
						</view>
						<view class="screen-popup-body">
							<view class="uni-list">
								<checkbox-group @change="checkboxChange">
									<label class="uni-list-cell uni-list-cell-pd flex flex-between border-bottom" v-for="(item, index) in templateAll"
									 :key="item.id">
										<view>{{item.gaugingPoint}}</view>
										<view>
											<checkbox :value="item.id" :checked="item.checked" />
										</view>
									</label>
								</checkbox-group>
							</view>
						</view>
					</view>
				</uni-popup>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		process,
		buildpost,
		submitReview,
		selectEquipment,
	} from '@/common/api/ConstructionReport.js'
	import {
		getFileInfo
	} from '@/common/api/api.js'
	import ipConfig from '@/utils/ipconfig.js'
	export default {
		data() {
			return {
				projectId: null,
				itemstab: [],
				current: 0,
				detailed: [],
				detailedList: {},
				InformationField: {
					subProjectId: null,
					detailId: null
				},
				InfoList: [],
				msgfrom: true,
				showBtn: false,
				showSub: false,
				showListBtn: true,
				patrolStatus: {
					processId: null,
					subProjectId: null
				},
				ProjectReportStorage: {
					id: null,
					processId: null,
					detailedId: null,
					subProjectId: null,
					processType: null,
					status: null,
				},
				ConstructionProjectStorage: {},
				templateAll: [],
				equipment: {
					currentDetailedProId: null,
					firstDetailedProId: null,
					firstProcessStandardProIds: null,
				},
			}
		},
		onLoad(option) {
			if (option.sort) {
				this.current = JSON.parse(option.sort)
			}
			if (option.subProjectId && option.processId && option.projectName) {
				this.patrolStatus.subProjectId = option.subProjectId
				this.patrolStatus.processId = option.processId
				this.patrolStatus.projectName = option.projectName
				this.msgfrom = false
				this.showBtn = false
				this.showSub = false
			}else{
				this.msgfrom = true
			}
		},
		onReady() {
			uni.removeStorageSync('InfoListData');
		},
		onShow() {
			uni.removeStorageSync('ProjectReportStorage')
			uni.removeStorageSync('InfoListData')
			if(this.msgfrom){
				this.ConstructionProjectStorage = uni.getStorageSync('ConstructionProjectStorage')
				this.projectId = this.ConstructionProjectStorage.projectId
				this.equipmentSteps(this.ConstructionProjectStorage)
			}else{
				this.equipmentSteps(this.patrolStatus)
			}
		},
		methods: {
			OperationGuideBtn(operationGuide) {
				if (operationGuide) {
					// plus.runtime.openURL(ipConfig.ip + 'admin/sys-file/getFileById?id=' + operationGuide, function(res) {
					// });
					uni.downloadFile({
						url: ipConfig.ip + 'admin/sys-file/getFileById?id='+operationGuide,
						success: function (res) {
							let filePath = res.tempFilePath;
							uni.openDocument({
								filePath: filePath,
								success: function (res) {
									console.log('打开文档成功');
								}
							});
						}
					});
				} else {
					uni.showToast({
						title: '暂无数据',
						icon: 'none'
					})
				}
			},
			SafetyRegulationsBtn(safetyRegulations) {
				if (safetyRegulations) {
					// plus.runtime.openURL(ipConfig.ip + 'admin/sys-file/getFileById?id=' + safetyRegulations, function(res) {
					// 	console.log(res);
					// });
					uni.downloadFile({
						url: ipConfig.ip + 'admin/sys-file/getFileById?id='+safetyRegulations,
						success: function (res) {
							let filePath = res.tempFilePath;
							uni.openDocument({
								filePath: filePath,
								success: function (res) {
									console.log('打开文档成功');
								}
							});
						}
					});
				} else {
					uni.showToast({
						title: '暂无数据',
						icon: 'none'
					})
				}
			},
			MaterialsBtn(materials) {
				if (materials) {
					// plus.runtime.openURL(ipConfig.ip + 'admin/sys-file/getFileById?id=' + materials, function(res) {
					// 	console.log(res);
					// });
					uni.showLoading({
						title: '文件加载中…'
					})
					uni.downloadFile({
						url: ipConfig.ip + 'admin/sys-file/getFileById?id='+materials,
						success: function (res) {
							uni.hideLoading();
							let filePath = res.tempFilePath;
							uni.openDocument({
								filePath: filePath,
								success: function (res) {
									console.log('打开文档成功');
								}
							});
						}
					});
				} else {
					uni.showToast({
						title: '暂无数据',
						icon: 'none'
					})
				}
			},
			goProcessProgress() { //工序进度
				console.log(this.detailedList)
				if (this.detailedList.InfoList.length > 0) {
					uni.navigateTo({
						url: '/pages/Application/ConstructionReport/ConstructionProject/ProjectReport/ProcessProgress/ProcessProgress?detailedProId=' +
							this.detailedList.processDetailedProId
					})
				} else {
					uni.showToast({
						title: '暂无工序进度',
						icon: 'none'
					})
				}
			},
			equipmentSteps(params) {
				this.itemstab = [];
				process(params.subProjectId, params.processId).then(res => {
					if (res.data) {
						this.detailed = res.data.detailedList
						this.detailed.forEach(obj => {
							this.itemstab.push(obj.subProcessName)
							if (this.current == obj.sort) {
								this.ProjectReportStorage.processType = obj.processType;
								this.equipment.currentDetailedProId = obj.processDetailedProId
								this.InformationField.detailId = obj.id
								this.InformationField.subProjectId = params.subProjectId

								buildpost(this.InformationField).then(res1 => {
									if (res1.data) {
										this.$set(obj, 'InfoList', res1.data)
										this.detailedList = obj
										this.ProjectReportStorage.subProjectId = params.subProjectId;
										this.ProjectReportStorage.processType = this.detailedList.processType;
										this.ProjectReportStorage.detailedId = this.detailedList.id;
										this.ProjectReportStorage.id = this.detailedList.id
										this.ProjectReportStorage.processId = res.data.processId
										this.InformationField.detailId = this.detailedList.id;
										if (this.current == 0) {
											this.equipment.firstDetailedProId = obj.processDetailedProId
											// let firstInfoList = res.data.filter(listObj => {
											// 	return listObj.status == 4
											// })
											uni.setStorageSync('firstInfoList', res1.data)
										}
										if(!this.msgfrom){
											this.showBtn = false;
											this.showSub = false;
										}else{
											if (obj.auditStatus == 4) { //切换所有
												this.showBtn = false;
												this.showSub = false;
											} else {
												this.showSub = true;
												if (this.current == 0) { //步骤1
													if (res.data.length > 0) {
														if(obj.auditStatus == 0 || obj.auditStatus == 3 || obj.auditStatus == 9 ){
															this.showBtn = true;
															this.showSub = true;
														}else{
															this.showBtn = res.data.every(listObj => { //第二层根据上传资料判断
																// return listObj.status != 3 || listObj.status != 4
																return listObj.status != 4
															})
														}
													} else {
														this.showBtn = true
													}
												} else {
													this.templateAll = uni.getStorageSync('firstInfoList')
													if (uni.getStorageSync('firstInfoList').length == 0) {
														this.showBtn = false
														this.showSub = false
													} else {
														if (this.detailedList.InfoList) {
															if (this.detailedList.InfoList.length == this.templateAll.length) {
																this.showBtn = false
															} else {
																this.showBtn = true
																// this.showBtn = res.data.every(listObj => {//第二层根据上传资料判断
																// 	// return listObj.status != 3 || listObj.status != 4
																// 	return listObj.status != 4
																// })
															}
															this.detailedList.InfoList.forEach(obj => {
																this.templateAll = this.templateAll.filter((obj2, index) => {
																	return obj.gaugingPoint != obj2.gaugingPoint
																})
															})
														}
													}
												}
											}
										}
									}
								})
							}
						})
					}
				})
			},
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex;
					if(this.msgfrom){
						this.equipmentSteps(this.ConstructionProjectStorage)
					}else{
						this.equipmentSteps(this.patrolStatus)
					}
				}
			},
			goConstructionData(item) {
				this.ProjectReportStorage.status = item.status
				uni.setStorageSync('ProjectReportStorage', this.ProjectReportStorage)
				uni.setStorageSync('InfoListData', item)
				uni.navigateTo({
					url: '/pages/Application/ConstructionReport/ConstructionProject/ProjectReport/ConstructionData/ConstructionData?sort=' +
						this.current+ '&msgfrom=' + this.msgfrom
				})
			},
			AddConstruction() {
				uni.removeStorageSync('InfoListData');
				this.equipment.firstProcessStandardProIds = null
				if (this.current == 0) {
					uni.setStorageSync('ProjectReportStorage', this.ProjectReportStorage)
					uni.navigateTo({
						url: '/pages/Application/ConstructionReport/ConstructionProject/ProjectReport/ConstructionData/ConstructionData?sort=' + this.current
					})
				} else {
					this.$refs.popup.open()
				}
			},

			checkboxChange(e) { //选择模板列表
				this.equipment.firstProcessStandardProIds = e.detail.value
			},
			close() {
				this.$refs.popup.close()
			},
			ChoiceTemplate() {
				if (this.templateAll.length > 0) {
					selectEquipment(this.equipment).then(res => {
						// this.templateAll = this.templateAll.filter((obj,index) => {
						//  return obj.id != this.equipment.firstProcessStandardProIds
						// })
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.equipmentSteps(this.ConstructionProjectStorage)
						this.$refs.popup.close()
					})
				} else {
					uni.showToast({
						title: '暂无数据可添加',
						icon: 'none'
					})
					this.$refs.popup.close()
				}

			},
			submitReviewBtn() {
				let submitId = {
					detailedProId: this.detailedList.InfoList[0].detailedProId,
					projectId: this.projectId,
					subProecssId: this.detailedList.id
				}
				let flag = this.detailedList.InfoList.some((item)=> {
					return item.status == -1 || item.status == 9
				})
				console.log(flag)
				if(flag){
					submitReview(submitId).then(res => {
						if (res.code == 20000) {
							uni.showToast({
								title: res.message
							})
							setTimeout(() => {
								this.equipmentSteps(this.ConstructionProjectStorage)
							}, 1000)
						}
					})
				}else{
					uni.showToast({
						title: '当前状态不能提交',
						icon: 'none',
					})
				}
			},
			openTip() {
				uni.showModal({
					title: '提示',
					content: '请保障一次添加完全部需要施工的测点',
					showCancel: false,
					success: function (res) {
						if (res.confirm) {
							console.log('用户点击确定');
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			}
		}
	}
</script>

<style lang="less">
	.project-report /deep/ .segmented-control {
		height: 100rpx;
	}

	.submit-btn {
		width: 90%;
		margin: 20rpx auto;
		height: 80rpx;
		line-height: 80rpx;
		font-size: 28rpx;
		background-color: #09AAE9;
		color: #fff;
		border-radius: 50rpx;
	}

	.screen-popup {
		background-color: #fff;
		min-height: 600rpx;
		max-height: 1200rpx;

		.screen-popup-title {
			background-color: #f2f2f2;
			height: 100rpx;
			line-height: 100rpx;
			color: #666;

			>view {
				text {
					display: block;
					padding: 0 40rpx;
					line-height: 100rpx;
				}

				text:last-child {
					background-color: #09AAE9;
					color: #fff;
				}
			}
		}
	}

	.screen-popup-body {
		/deep/ .uni-searchbar__box {
			border-color: #eee
		}

		.uni-list {
			max-height: 980rpx;
			overflow-y: auto;

			uni-label {
				padding: 20rpx 3%;
			}
		}
	}

	.explain /deep/ .uni-list-item__content-title {
		overflow: hidden;
		display: -webkit-box !important;
		-webkit-line-clamp: 1;
		-webkit-box-orient: vertical;
		word-break: break-all;
		line-height: 50rpx;
	}



	/* #ifndef APP-PLUS */
	page {
		width: 100%;
		min-height: 100%;
		display: flex;
	}

	/* #endif */

	.tabs {
		flex: 1;
		flex-direction: column;
		overflow: hidden;
		background-color: #ffffff;
		/* #ifdef MP-ALIPAY || MP-BAIDU */
		height: 100vh;
		/* #endif */
	}

	.scroll-h {
		width: 750rpx;
		/* #ifdef H5 */
		width: 100%;
		/* #endif */
		height: 80rpx;
		flex-direction: row;
		/* #ifndef APP-PLUS */
		white-space: nowrap;
		/* #endif */
		/* flex-wrap: nowrap; */
		/* border-color: #cccccc;
		border-bottom-style: solid;
		border-bottom-width: 1px; */
	}

	.line-h {
		height: 1rpx;
		background-color: #cccccc;
	}

	.uni-tab-item {
		/* #ifndef APP-PLUS */
		display: inline-block;
		/* #endif */
		flex-wrap: nowrap;
		padding-left: 34rpx;
		padding-right: 34rpx;
	}

	.uni-tab-item-title {
		color: #555;
		font-size: 30rpx;
		height: 80rpx;
		line-height: 80rpx;
		flex-wrap: nowrap;
		/* #ifndef APP-PLUS */
		white-space: nowrap;
		/* #endif */
	}

	.uni-tab-item-title-active {
		color: #007AFF;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		flex: 1;
		flex-direction: row;
	}

	.scroll-v {
		flex: 1;
		/* #ifndef MP-ALIPAY */
		flex-direction: column;
		/* #endif */
		width: 750rpx;
		width: 100%;
	}

	.update-tips {
		position: absolute;
		left: 0;
		top: 41px;
		right: 0;
		padding-top: 5px;
		padding-bottom: 5px;
		background-color: #FDDD9B;
		align-items: center;
		justify-content: center;
		text-align: center;
	}

	.update-tips-text {
		font-size: 14px;
		color: #ffffff;
	}

	.refresh {
		width: 750rpx;
		width: 100%;
		height: 64px;
		justify-content: center;
	}

	.refresh-view {
		flex-direction: row;
		flex-wrap: nowrap;
		align-items: center;
		justify-content: center;
	}

	.refresh-icon {
		width: 30px;
		height: 30px;
		transition-duration: .5s;
		transition-property: transform;
		transform: rotate(0deg);
		transform-origin: 15px 15px;
	}

	.refresh-icon-active {
		transform: rotate(180deg);
	}

	.loading-icon {
		width: 20px;
		height: 20px;
		margin-right: 5px;
		color: #999999;
	}

	.loading-text {
		margin-left: 2px;
		font-size: 16px;
		color: #999999;
	}

	.loading-more {
		align-items: center;
		justify-content: center;
		padding-top: 10px;
		padding-bottom: 10px;
		text-align: center;
	}

	.loading-more-text {
		font-size: 28rpx;
		color: #999;
	}
</style>
